#!/bin/bash


echo "-------------java端部署------------"
# 输出当前正在运行的进程
echo "正在运行的进程："
ps aux | grep java | grep -v grep

# 停止已经运行的进程
echo "停止进程："
ps aux | grep java | grep -v grep | awk '{print $2}' | while read pid
do
  echo "停止进程 $pid"
  kill $pid
done

sleep 5

# 检查进程是否停止完全
while ps aux | grep java | grep -v grep >/dev/null;
do
    sleep 1
    echo "等待进程停止..."
done

# 拉取最新代码
echo "拉取最新代码..."
cd /data/myapp/my-app-backend
git pull
echo "打包..."
mvn clean package
cd ../
# 启动Java程序
echo "启动进程..."
nohup java -jar my-app-backend/target/my-app-backend-0.0.1-SNAPSHOT.jar >/dev/null 2>&1 -xms1024m -xmx1024m &

# 输出最新启动的进程号
echo "最新启动的进程号："
ps aux | grep java | grep -v grep | awk '{print $2}'
tail -f /data/myapp/logs/myapp.log


